﻿<nav class="navbar-default navbar-static-side" role="navigation">
    <div class="nav-close">
        <i class="fa fa-times-circle"></i>
    </div>
    <div class="sidebar-collapse">
        <ul class="nav" id="side-menu">
            <li class="nav-header">
                <div class="dropdown profile-element">
                    <a data-toggle="dropdown" class="dropdown-toggle" href="javascript:void(0)">
                        <span class="clear">
                            <span class="block m-t-xs" style="font-size:20px;">
                                <i class="fa fa-area-chart"></i>
                                <strong class="font-bold">后台管理</strong>
                            </span>
                        </span>
                    </a>
                </div>
                <div class="logo-element">
                    Admin
                </div>
            </li>
            <li>
                <a class="J_menuItem" href="index_v1.html">
                    <i class="fa fa-home"></i>
                    <span class="nav-label">主页</span>
                </a>
            </li>
            <li class="line dk"></li>



        </ul>
    </div>
</nav>
<script src="~/Scripts/jquery.min.js"></script>

<script>
    $(function () {
        loadFirstMenu();
    })

    function loadFirstMenu() {
        $.ajax({
            type: "post",
            url: "/Sys_Menu/GetMenuById",
            contentType: "application/json",
            dataType: "json",
            async: false,
            data: JSON.stringify({ pId: 0 }),
            success: function (da) {
                $.each(da, function (index, ele) {
                    var $li = $("<li >"
                        + " <a href='#'><i class='" + ele.icon + "'></i>"
                        + " <span class='nav-label'>" + ele.MenuName + "</span>"
                        + " <span class='fa arrow'></span>"
                        + " </a>"
                        + " <ul class='nav nav-second-level'>"
                        + loadChildMenu(ele.Id)
                        + " </ul></li>");

                    $("#side-menu").append($li);
                });
            },
            error: function (xhr) { xhr.responseText }
        });
    }

    function loadChildMenu(pid) {

        var menu = "";

        if (sessionStorage.getItem(pid) == null) {
            $.ajax({
                type: "post",
                url: "/Sys_Menu/GetMenuById",
                contentType: "application/json",
                dataType: "json",
                async: false,
                data: JSON.stringify({ pId: pid }),
                success: function (da) {
                    sessionStorage.setItem(pid, JSON.stringify(da));
                },
                error: function (xhr) { xhr.responseText }
            });
        }


        $.each(JSON.parse(sessionStorage.getItem(pid)), function (index, ele) {
            menu += " <li>"
            menu += "      <a class='J_menuItem' href='" + ele.Url + "'>" + ele.MenuName + "</a>"
            menu += " </li>";
        });

        return menu;
    }
</script>
